<?php

require_once( 'flexical/admin/format-response.php' );
require_once( 'flexical/admin/controller.php' );

class Flexical_Admin_Format_Controller extends Flexical_Admin_Controller
{
	public function respond( $request )
	{
		$response = null;

		switch ( get_class( $request ) )
		{
			case 'Flexical_Admin_Show_Formats_Request':
				$response = $this->show_formats( false );
				break;

			case 'Flexical_Admin_Create_Format_Request':
				$response = $this->create_format( $request->format );
				break;

			case 'Flexical_Admin_Update_Format_Request':
				$response = $request->canceled ? $this->show_formats( $request->canceled ) : $this->update_format( $request->format );
				break;

			case 'Flexical_Admin_Delete_Format_Request':
				$response = $request->canceled ? $this->show_formats( $request->canceled ) : $this->delete_format( $request->format );
				break;

			case 'Flexical_Admin_View_Format_Request':
				$response = $this->view_format( $request->format );
				break;

			case 'Flexical_Admin_Edit_Format_Request':
				$response = $this->edit_format( $request->format );
				break;

			case 'Flexical_Admin_Confirm_Delete_Format_Request':
				$response = $this->confirm_delete_format( $request->format );
				break;

			case 'Flexical_Admin_Delete_All_Formats_Request':
				$response = $this->delete_all_formats( $request->confirmed );
				break;

			default:
				throw new Exception( "Unsupported request class: " . get_class( $request ) );
		}

		return $response;
	}

	public function show_formats( $canceled )
	{
		return new Flexical_Admin_Show_Formats_Response( $this->get_dao()->read_formats(), $canceled );
	}

	public function create_format( $format )
	{
		$new_format = $this->get_dao()->create_format( $format );
		$formats = $this->get_dao()->read_formats();

		return new Flexical_Admin_Create_Format_Response( $formats, $new_format );
	}

	public function update_format( $format )
	{
		$this->get_dao()->update_format( $format );
		$formats = $this->get_dao()->read_formats();

		return new Flexical_Admin_Update_Format_Response( $formats, $format );
	}

	public function delete_format( $format )
	{
		$old_format = $this->get_dao()->read_format( $format->id );
		$this->get_dao()->delete_format( $format->id );
		$formats = $this->get_dao()->read_formats();

		return new Flexical_Admin_Delete_Format_Response( $formats, $old_format );
	}

	public function view_format( $format )
	{
		return new Flexical_Admin_View_Format_Response( $this->get_dao()->read_formats(), $this->get_dao()->read_format( $format->id ) );
	}

	public function edit_format( $format )
	{
		return new Flexical_Admin_Edit_Format_Response( $this->get_dao()->read_format( $format->id ) );
	}

	public function confirm_delete_format( $format )
	{
		$listings_using_format = $this->get_listings_using_format( $format->id );

		if ( sizeof( $listings_using_format ) > 0 )
		{
			return new Flexical_Admin_Cant_Delete_Format_In_Use_Response( $this->get_dao()->read_format( $format->id ),
				$listings_using_format, $this->get_dao()->read_formats() );
		}

		else
		{
			return new Flexical_Admin_Confirm_Delete_Format_Response( $this->get_dao()->read_format( $format->id ) );
		}
	}

	public function delete_all_formats( $confirmed )
	{
		$listings_using_formats = $this->get_listings_using_formats();

		if ( sizeof( $listings_using_formats ) > 0 )
		{
			return new Flexical_Admin_Cant_Delete_Formats_In_Use_Response( $listings_using_formats,
				$this->get_dao()->read_formats() );
		}

		else if ( $confirmed )
		{
			foreach ( $this->get_dao()->read_formats() as $id => $format )
			{
				$this->get_dao()->delete_format( $id );
			}
		}

		return new Flexical_Admin_Delete_All_Formats_Response( $this->get_dao()->read_formats(), $confirmed );
	}

	private function get_listings_using_format( $format_id )
	{
		$listings = array();

		foreach ( $this->get_dao()->read_listings() as $listing_id => $listing )
		{
			if ( $listing->format_id == $format_id )
			{
				$listings[ $listing_id ] = $listing;
			}
		}

		return $listings;
	}

	private function get_listings_using_formats()
	{
		$listings = array();

		foreach ( $this->get_dao()->read_formats() as $format_id => $format )
		{
			foreach ( $this->get_listings_using_format( $format_id ) as $listing_id => $listing )
			{
				$listings[ $listing_id ] = $listing;
			}
		}

		return $listings;
	}
}

?>
